//
//  PLUserGuideView.m
//  PlatformCommon
//
//  Created by KimmanYip on 2017/10/27.
//

#import "PLUserGuideView.h"
#import "PLMacro.h"
#import "UIView+PLExtension.h"

@interface PLUserGuideView ()

@property (nonatomic, strong) UIViewController        *controller;
@property (nonatomic, strong) UIView        *forView;
@property (nonatomic, strong) UIColor       *bolderColor;
@property (nonatomic, strong) UIImage       *indicator;
@property (nonatomic, strong) UIImageView   *designSketchView;
@property (nonatomic, strong) UIView        *tabBarMask;    // tabbar遮罩

@property (nonatomic, assign) CGPoint       indicatorAt;
@property (nonatomic, assign) CGPoint       sketchAt;

@property (nonatomic, copy) UserGuideDismissed dismissed;

@property (nonatomic, strong) CAShapeLayer  *shapeLayer;


@end

@implementation PLUserGuideView

#pragma mark - Public
- (void)showGuideInViewController:(UIViewController *)controller
                          forView:(UIView *)forView
                      bolderColor:(UIColor *)bolderColor
                        indicator:(UIImage *)indicator
                     designSketch:(UIImageView *)sketch
                      indicatorAt:(CGPoint)indicatorAt
                        completed:(UserGuideDismissed)completed {
    self.controller     = controller;
    self.forView        = forView;
    self.bolderColor    = bolderColor;
    self.indicator      = indicator;
    self.designSketchView   = sketch;
    self.indicatorAt    = indicatorAt;
    self.dismissed      = completed;
    
    [self setupUserGuide];
}

- (void)showGuideInViewController:(UIViewController *)controller
                           sketch:(UIImageView *)sketch
                         sketchAt:(CGPoint)sketchAt
                        indicator:(UIImage *)indicator
                      indicatorAt:(CGPoint)indicatorAt
                        completed:(UserGuideDismissed)completed {
    self.controller         = controller;
    self.designSketchView   = sketch;
    self.sketchAt           = sketchAt;
    self.indicator          = indicator;
    self.indicatorAt        = indicatorAt;
    self.dismissed          = completed;
    
    [self setupUserGuide];
}

#pragma mark - Private
- (void)setupUserGuide {
    
    // 禁用返回手势
    if ([self.controller.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.controller.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
    
    // 移除所有子视图
    for (UIView *subView in [self subviews]) {
        [subView removeFromSuperview];
    }
    
    [self.controller.view addSubview:self];
    CGRect frame = CGRectMake(self.controller.view.frame.origin.x, self.controller.view.frame.origin.y, self.controller.view.frame.size.width, kScreenHeight);
    self.frame = frame;
    
    // tabbar遮罩
    UIWindow *keyWindow = (UIWindow *)[UIApplication sharedApplication].keyWindow;
    self.tabBarMask = [UIView new];
    self.tabBarMask.backgroundColor = [UIColor colorWithWhite:0 alpha:0.8];
    [keyWindow addSubview:self.tabBarMask];
    self.tabBarMask.frame = CGRectMake(0, keyWindow.bounds.size.height-kTabbarHeight, keyWindow.bounds.size.width, keyWindow.bounds.size.height);
    
    // 点击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
    [self addGestureRecognizer:tap];
    
    if (self.designSketchView) {
        [self addSubview:self.designSketchView];
        self.designSketchView.center = self.sketchAt;
    } else {
        // path
        UIBezierPath *orgPath = [UIBezierPath bezierPathWithRect:[self.controller.view convertRect:_forView.frame toView:self.controller.view]];
        UIBezierPath *selfPath = [UIBezierPath bezierPathWithRect:self.bounds];
        [selfPath appendPath:[orgPath bezierPathByReversingPath]];
        self.shapeLayer.path = selfPath.CGPath;
        
        [self.layer setMask:self.shapeLayer];
    }
    
    // 指示图标
    if (self.indicator) {
        UIImageView *indicatorView = [[UIImageView alloc] initWithImage:self.indicator];
        [self addSubview:indicatorView];
        indicatorView.x = self.indicatorAt.x;
        indicatorView.y = self.indicatorAt.y;
    }
}

- (void)dismiss {
    // 启用返回手势
    if ([self.controller.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.controller.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
    [self removeFromSuperview];
    [self.tabBarMask removeFromSuperview];
    if (self.dismissed) {
        self.dismissed();
    }
}

#pragma Life Cycle
- (instancetype)init {
    self = [super init];
    if (self) {
        self.shapeLayer = [CAShapeLayer layer];
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.8];
    }
    return self;
}


@end
